package com.leather.admin.vo;

import com.leather.admin.entity.Permission;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

@Data
public class PermissionVo implements Serializable {

    private Integer parentId;

    /**
     * 判断是否父级菜单 0 表示是 1 不是
     * isNullAble:1
     */
    private Integer isParent;

    /**
     * 权限名称
     * isNullAble:1
     */
    private String permissionName;

    /**
     * 权限路径
     * isNullAble:1
     */
    private String permissionUrl;


    private String permissionValue;

    /**
     * 编号
     */
    private Integer id;


    public Result validate() {
        if (StringUtils.isBlank(permissionName)) {
            return Result.error(CodeMsg.EMPTY_PERMISSION_NAME);
        }
        if (isParent != 1) {
            if (StringUtils.isBlank(permissionValue)) {
                return Result.error(CodeMsg.EMPTY_PERMISSION_VALUE);
            }
            if (StringUtils.isBlank(permissionValue)) {
                return Result.error(CodeMsg.EMPTY_PERMISSION_URL);
            }
        }
        return Result.success(transfer());
    }

    public Permission transfer() {
        Permission permission = new Permission();
        permission.setId(id==null?0:id);
        permission.setPermissionName(permissionName);
        permission.setPermissionUrl(permissionUrl);
        permission.setPermissionValue(permissionValue);
        permission.setParentId(parentId);
        if (parentId == 0) {
            permission.setIsParent(0);
        } else {
            permission.setIsParent(isParent-1);
        }
        return permission;

    }

}
